package my.simplecalculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	 private static<T> void  p(T x){
		System.out.println(x);
	}
	private double ans=0;
	private View op;
	private boolean startover=true;
	public void num(View v){
		TextView textview = (TextView) findViewById(R.id.textView1);
		String origin;
    	if(startover){
			startover=false;
			origin="";
		}
    	else 
    		origin=textview.getText().toString();
		String strx=((Button)v).getText().toString();
    	textview.setText(origin+strx);
    	p("number recieve : " + strx);
	}
	public void operator(View v){
		getans(v);
	}
	private boolean testEqual(int id,View v){
		p(findViewById(id) + " "+v);
		return findViewById(id)==v;
	}
	public void getans(View v){
		if(startover){
			op=v;
			return;
		}
		TextView textview = (TextView) findViewById(R.id.textView1);
    	double number;
    	try{
    	number=Double.parseDouble(textview.getText().toString());
    	if(testEqual(R.id.buttonplus,op)){
    		p("hit +" );
    		ans+=number;
    	}
    	else if(testEqual(R.id.buttonminus,op)){
    		p("hit -" );
    		ans-=number;
    	}
    	else if(testEqual(R.id.buttontimes,op)){
    		p("hit *");
    		ans*=number;
    	}
    	else if(testEqual(R.id.buttondivide,op)){
    		p("hit /");
    		ans/=number;
    	}
    	else{
    		p("hit none : ");
    		ans=number;
    	}
    	textview.setText(ans+"");
    	p(ans);
    	}
    	catch(java.lang.NumberFormatException e){
    		textview.setText("");
    		ans=0;
    	}
    	op=v;
    	startover=true;
	}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
